<template>
  <div v-loading="loading">
    <el-form :inline="true" :model="roleFm" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="roleFm.roleName" placeholder="角色名称" />
      </el-form-item>
      <el-form-item>
        <el-button type="info" @click="handleGetRoleList">搜索</el-button>
        <el-button type="primary" @click="handleOpenRoleInsert">新增</el-button>
        <el-button type="warning" @click="handleOpenRoleEdit">编辑</el-button>
        <el-button type="danger" @click="handleOpenRoleDelete">删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      ref="singleTable"
      :data="tableData"
      highlight-current-row
      style="width: 100%"
      @current-change="handleTableChange"
    >
      <el-table-column
        prop="roleId"
        label="角色编号"
        width="180"
      />
      <el-table-column
        prop="roleName"
        label="角色名称"
        width="180"
      />
      <el-table-column
        prop="createTime"
        label="创建时间"
      />
    </el-table>
    <el-pagination
      :current-page="roleFm.pageNum"
      :page-sizes="[10, 20 ,30]"
      :page-size="roleFm.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="roleFm.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="saveRole" label-width="100px">
        <el-form-item label="角色名称：">
          <el-input v-model="saveRole.roleName" />
        </el-form-item>
        <el-form-item label="所属部门：">
          <el-input v-model="saveRole.deptName" readonly @focus="deptDialogVisible=true" />
          <el-input v-show="false" v-model="saveRole.deptId" />
          <el-dialog
            append-to-body
            :visible.sync="deptDialogVisible"
            width="30%"
            :before-close="handleClose"
          >
            <el-tree ref="dept" node-key="deptId" accordion :data="depts" :props="DeptdefaultProps" @node-click="handleDeptNodeClick" />
            <span slot="footer" class="dialog-footer">
              <el-button @click="deptDialogVisible = false">取 消</el-button>
              <el-button type="primary" @click="deptDialogVisible = false">确 定</el-button>
            </span>
          </el-dialog>
        </el-form-item>
        <el-form-item label="备注：">
          <el-input
            v-model="saveRole.remark"
            type="textarea"
            :rows="2"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="功能权限：">
          <el-tree ref="tree" v-model="saveRole.menuIds" node-key="menuId" :data="menus" show-checkbox :props="defaultProps" @node-click="handleNodeClick" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button v-if="handleShowInsertButton" type="primary" @click="handleRoleInsert">新 增</el-button>
        <el-button v-if="handleShowEditButton" v-loading="updLoad" type="primary" @click="handleRoleEdit">编 辑</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  name: 'RoleList',
  data() {
    return {
      updLoad: false,
      deptDialogVisible: false,
      loading: true,
      DeptdefaultProps: {
        value: 'deptId',
        children: 'childMenus',
        label: 'name'
      },
      defaultProps: {
        value: 'menuId',
        children: 'menuList',
        label: 'name'
      },
      title: '',
      dialogVisible: false,
      handleShowInsertButton: false,
      handleShowEditButton: false,
      tableRow: {},
      tableData: [],
      saveRole: {
        menuIds: []
      },
      roleFm: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
        roleName: ''
      },
      depts: [],
      menus: []
    }
  },
  created() {
    this.handleGetRoleList()
    this.handleGetDeptList()
    this.handleGetMenuList()
  },
  methods: {
    handleDeptNodeClick(data) {
      this.saveRole.deptId = data.deptId
      this.saveRole.deptName = data.name
      this.deptDialogVisible = false
    },
    handleRoleEdit() {
      this.updLoad = true
      this.saveRole.menuIds = this.$refs.tree.getCheckedKeys()
      axios.post('http://localhost:8200/sys/role/edit', this.saveRole).then(res => {
        if (res.data.code === '200') {
          this.$message({
            message: res.data.msg,
            type: 'success'
          })
          this.dialogVisible = false
          this.handleGetRoleList()
        } else {
          this.$message({
            message: res.data.msg,
            type: 'error'
          })
        }
        this.updLoad = false
      })
    },
    handleRoleInsert() {
      // 获取复选框key
      // const aaa = []
      this.saveRole.menuIds = this.$refs.tree.getCheckedKeys()
      axios.post('http://localhost:8200/sys/role/insert', this.saveRole).then(res => {
        if (res.data.code === '200') {
          this.$message({
            message: res.data.msg,
            type: 'success'
          })
          this.dialogVisible = false
          this.handleGetRoleList()
          console.log("新增角色成功结束")
        } else {
          this.$message({
            message: res.data.msg,
            type: 'error'
          })
          console.log("新增角色结束")
        }
      })
    },
    handleOpenDeptTree() {
      alert('dadad')
      this.deptDialogVisible = true
    },
    handleOpenRoleDelete() {
      if (this.tableRow.roleId === null) {
        alert('请选择')
        return
      } else {
        if (confirm('是否删除')) {
          axios.post('http://localhost:8200/sys/role/delete', this.tableRow).then(res => {
            if (res.data.code === '200') {
              this.$message({
                message: res.data.msg,
                type: 'success'
              })
              this.handleGetRoleList()
            } else {
              this.$message({
                message: res.data.msg,
                type: 'error'
              })
            }
          })
        }
      }
    },
    handleOpenRoleEdit() {
      if (this.tableRow.roleId === null) {
        alert('请选择')
        return
      } else {
        this.title = '编辑'
        this.saveRole = this.tableRow
        this.dialogVisible = true
        this.handleShowInsertButton = false
        this.handleShowEditButton = true
        this.saveRole.deptName = this.saveRole.roleDept.name
        this.saveRole.deptId = this.tableRow.deptId
        // 回显树
        this.$nextTick(() => {
          this.$refs.tree.setCheckedKeys(this.saveRole.menuIds)
        })
      }
    },
    handleOpenRoleInsert() {
      if (this.tableRow.roleId != null) {
        alert('请勿选择')
        this.setCurrent(null)
        this.tableRow = {}
        return
      } else {
        this.title = '新增'
        this.dialogVisible = true
        this.handleShowInsertButton = true
        this.handleShowEditButton = false
      }
    },
    handleNodeClick(data) {
      console.log(data)
    },
    setCurrent(row) {
      this.$refs.singleTable.setCurrentRow(row)
    },
    handleTableChange(val) {
      this.tableRow = val
    },
    handleSizeChange(val) {
      this.roleFm.pageSize = val
      this.handleGetRoleList()
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      this.roleFm.pageNum = val
      this.handleGetRoleList()
      console.log(`当前页: ${val}`)
    },

    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
          this.saveRole = {}
          this.$nextTick(() => {
            this.$refs.tree.setCheckedKeys([])
          })
        })
        .catch(_ => {})
    },
    handleGetRoleList() {
      axios.post('http://localhost:8200/sys/role/list', this.roleFm).then(res => {
        console.log(res)
        this.tableData = res.data.obj.list
        this.roleFm.pageNum = res.data.obj.pageNum
        this.roleFm.pageSize = res.data.obj.pageSize
        this.roleFm.total = res.data.obj.total
        this.loading = false
      })
    },
    handleGetDeptList() {
      axios.post('http://localhost:8200/sys/dept/getTree').then(res => {
        console.log('用户编辑部门返回值：', res.data)
        this.depts = res.data
        console.log(this.depts)
      })
    },
    handleGetMenuList() {
      axios.post('http://localhost:8200/sys/menu/list').then(res => {
        this.menus = res.data.obj
        console.log('菜单：' + this.menus)
      })
    }
  }
}
</script>

<style scoped>

</style>
